package com.lhyl.front.web.controller;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lhyl.front.common.ClientInfo;
import com.lhyl.front.common.StringUtils;
import com.lhyl.front.constant.FrontConstant;
import com.lhyl.front.constant.UserConstant;
import com.lhyl.front.model.PageModel;
import com.lhyl.front.web.shiro.model.User;
import com.lhyl.front.web.shiro.model.UserLoginLog;
import com.lhyl.front.web.shiro.service.UserLoginLogService;
import com.lhyl.front.web.shiro.service.UserService;

/**
 * 首页
 * 
 * @author GusonYang
 * 
 */
@Controller
public class HomeController extends BaseController {
	
	
	private static final Logger logger = LogManager
			.getLogger(BaseController.class.getName());

	@Autowired
	private UserLoginLogService userLoginLogService;
	
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/home")
	public String UserList(HttpServletRequest req, ModelMap modelMap)
			throws Exception {
		super.initBaseData(modelMap);

		return "home";
	}

	@RequestMapping("/content/home")
	public String ContentHome(ModelMap modelMap) throws Exception {
		return "content-home";
	}

	@RequestMapping("/content/message/info")
	public String ContentMessage() {
		return "message/info";
	}

	@RequestMapping("content/help/info")
	public String ContentHelper() {
		return "help/info";
	}

	@RequestMapping(value = "/loginLog")
	public String loginLog(HttpServletRequest req, ModelMap modelMap)
			throws Exception {
		super.initBaseData(modelMap);

		try {

			User user = getLoginUserObject();
			UserLoginLog userlog = new UserLoginLog();

			ClientInfo info = new ClientInfo(req);

			userlog.setUid(user.getUid());
			userlog.setUname(user.getLoginName());
			userlog.setNavigator(info.getExplorerName());
			userlog.setOperationSystem(info.getOSName());
			userlog.setLoginAddress(info.getIpAddr());
			userlog.setLoginDate(new Date());

			userLoginLogService.insert(userlog);
			
			// 设置用户在线
			userService.setUserLine(user.getLoginName(), UserConstant.LINE);
			
		} catch (Exception e) {
			logger.error("写登陆日志失败" + e.getMessage());
		}
		return "redirect:/home";
	}

	@RequestMapping(value = "/home/queryAwardList")
	public void queryAwardList(HttpServletRequest req, HttpServletResponse response) throws Exception 
	{
		try {
			List<Map> result = this.reportService.getReportResult("queryAwardList", Collections.EMPTY_MAP);
			this.renderMsg(response, FrontConstant.SUCCESS_CODE, StringUtils.EMPTY,result);
		} catch (Exception e) {
			this.renderMsg(response, FrontConstant.FAILED_CODE, e.getMessage(),
					StringUtils.EMPTY);
		}
	}
}
